package com.llmcu.graphic.designpatterns.command;

import com.llmcu.graphic.designpatterns.command.test.MyButton;
import com.llmcu.graphic.designpatterns.command.test.MyCanvas;
import com.llmcu.graphic.designpatterns.command.test.MyJFrame;

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.Objects;

/**
 * xxxx
 *
 * @author liuling
 * @date 2024/7/20 16:34
 */
public class MemoryDrawFrame extends MyJFrame {
    public static final String RM = "清除存储";
    public static final String RE_PAINT = "重画";
    private MacroDrawCommand macroDrawCommand;
    private MyButton repaintButton;
    private MyButton removeMemoryButton;

    public MemoryDrawFrame(MyCanvas canvas, MyButton clearButton, String title)  {
        super(canvas, clearButton, title);
        macroDrawCommand = new MacroDrawCommand();
        repaintButton = new MyButton(RE_PAINT);
        removeMemoryButton = new MyButton(RM);
        this.getButtonBox().add(repaintButton);
        this.getButtonBox().add(removeMemoryButton);
        repaintButton.addActionListener(this);
        removeMemoryButton.addActionListener(this);

    }
    @Override
    public void mouseDragged(MouseEvent e) {
        super.mouseDragged(e);
        Command command = new DrawCommand(e.getX(),e.getY(),this.getCanvas());
        macroDrawCommand.add(command);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        MyButton button = (MyButton)(e.getSource());
        String text = button.getText();
        if(Objects.equals(text,"clear")){
            this.getCanvas().repaint();
        }else if (Objects.equals(text,RE_PAINT)){
            macroDrawCommand.execute();
        }else if (Objects.equals(text, RM)){
            macroDrawCommand.clear();
        }
    }
}
